package com.cweb.admin.model.bbs.bbs_comment_info.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cweb.admin.model.bbs.bbs_comment_info.entity.BbsCommentInfo;
import com.cweb.admin.model.bbs.bbs_comment_info.mapper.BbsCommentInfoMapper;
import com.cweb.admin.serivce.authority.UserLoginService;
import com.cweb.framework.base.serivce.BaseMybatisService;
import com.cweb.framework.pojo.PageParam;
import com.cweb.framework.util.MybatisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 论坛-评论信息 服务实现类
 *
 * @since 2020-09-10
 */
@Service
public class BbsCommentInfoService extends BaseMybatisService<BbsCommentInfoMapper, BbsCommentInfo> {
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 分页查询
     */
    public Object getList(BbsCommentInfo param, PageParam pageParam) {
        return this.getBaseMapper().listPage(MybatisUtil.initCondPage(pageParam), param);
    }

    /**
     * 新增评论
     */
    @Transactional
    public Object make(BbsCommentInfo param) {
        // 父评论回复数累计
        if (null != param.getParent()){
            this.getBaseMapper().updateReplyNumAdd(param.getParent());
        }
        // 新增评论
        BbsCommentInfo bbsCommentInfo = new BbsCommentInfo();
        bbsCommentInfo.setUserId(userLoginService.getCurLoginUserId());
        bbsCommentInfo.setArticleId(param.getArticleId()); // 文章id
        bbsCommentInfo.setContent(param.getContent()); // 内容
        bbsCommentInfo.setReplyNum(0); // 回复数，默认为为零
        bbsCommentInfo.setParent(param.getParent()); // 父评论
        bbsCommentInfo.setReplyUserId(param.getReplyUserId()); // 父评论
        return this.save(bbsCommentInfo);
    }
}
